package leetcode.code337;

import leetcode.helper.tree.TreeNode;

public class Solution {

	// [3,2,3,null,3,null,1]

	public int rob(TreeNode root) {
		return robNode(root);
	}

	/**
	 * 抢节点
	 *
	 * @param node
	 * @return
	 */
	public int robNode(TreeNode node) {
		if (node == null)
			return 0;
		int r1 = 0; // 包含头
		int r2 = 0; // 不包含头
		r1 = node.val + robNodeChild(node.left) + robNodeChild(node.right);
		r2 = robNode(node.left) + robNode(node.right);
		return Math.max(r1, r2);
	}

	/**
	 * 抢孩子
	 *
	 * @param node
	 * @return
	 */
	public int robNodeChild(TreeNode node) {
		if (node == null) {
			return 0;
		}
		return robNode(node.left) + robNode(node.right);
	}

}
